﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public delegate void ItemDataBound(object sender, ListViewItemEventArgs e);
public delegate void ItemClick(object sender, ListViewCommandEventArgs e);
public partial class UserControl_SlidingControl : System.Web.UI.UserControl
{
    public event ItemDataBound ItemDatabound;
    public event ItemClick ItemClick;
    # region Properties

    public bool IsVartical { get; set; }
    #region Paging Properties
    //Set PagesSize
    public Int16 PageSize { get; set; }

    //Paging feature is show and Hide Ex. Page 1  2 3 4 or Images
    public bool IsPaging { get; set; }

    //Overall Pages Count
    public int TotalPageCount { get; set; }

    //Paging show in Writen Ex: 1 of 4
    public string PagingWritenId { get; set; }

    //If Paging is true and PagingInImage Property also true. 
    //show Paging in Images.its take URL of selected Image.
    public string SelectedImageUrl { get; set; }

    //If Paging is true and PagingInImage Property also true. 
    //show Paging in Images.its take URL of Unselected Image.
    public string UnSelectedImageUrl { get; set; }

    //here set the Pages Show in Images or in No.
    public bool PagingInImages { get; set; }

    //Paging show in that Container ID
    public string PagingContainerId { get; set; }
    #endregion

    #region Grouping of Template
    //Set Records in One Group Template
    public bool IsGroupTemplate { get; set; }
    //If IsGrouping property True then No Of Items in Group Template
    public Int16 GroupItemCount { get; set; }
    #endregion

    //Its Template.Like List view Template
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate Template { get; set; }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate SelectedTemplate { get; set; }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate EmptyTemplate { get; set; }

    //DataSource set to ListView
    public object DataSource { get; set; }

    //Its Updated By Ajax Calling.. Give Complete Path of the User Control
    public string ControlPath { get; set; }

    //Paging show in that NextPage ID
    public string NextPageClientId { get; set; }

    //Paging show in that Previous Page ID
    public string PreviousPageClientId { get; set; }

    public int SelectedIndex { get; set; }

    public string CssClass { get; set; }

    public string ProgressBarId { get; set; }

    #endregion

    public override void RenderControl(HtmlTextWriter writer)
    {
        base.RenderControl(writer);
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {

            //if (Convert.ToString(Com.CDN.BCL.Helper.SessionHandler.GetItem("Dept_Id")) != "0")
            //{
            //    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert_1", "resetStats();", true);
            //}
        }


        //if (!Convert.ToBoolean(Com.CDN.BCL.Helper.SessionHandler.GetItem("EnabledAjax")))
        //{
        //    if (!Page.IsPostBack)
        //        FillList();
        //}
        //else
        //{
        FillList();
        //   }
    }

    public void ReInitializeJavaValue()
    {
        if (Page.IsPostBack)
        {

            string keyValue = "var setting;function intSetting() {setting = {IsGrouping:'" + IsGroupTemplate + "',GroupingCount:'" + GroupItemCount + "',isPaging: '" + IsPaging + "',PagingInImages: '" + PagingInImages + "',IsVartical: '" + IsVartical + "',NavigationControl: ('" + PagingContainerId + "'),$SlidingControl: $('#" + ScrollingDIV.ClientID + "'),SelectedImage: '" + SelectedImageUrl + "',DisSelectImage: '" + UnSelectedImageUrl + "',CurrentState: $('#" + CurrentState.ClientID + "'),PreviousState: $('#" + PreviousState.ClientID + "'),TotalPageCount: '" + TotalPageCount + "',ControlPath: '" + ControlPath + "',PagingWritenID: '" + PagingWritenId + "',ProgressBarID: '" + ProgressBarId + "',NextPageClientName: '" + NextPageClientId + "', PreviousPageClientName: '" + PreviousPageClientId + "'};} intSetting(); var sett = setting;intSpeed = -50; initHTML(sett); intSpeed = 500;setting=null;sett=null;";

            string Key = "Pagingkey" + DateTime.Now.ToOADate();
            ScriptManager.RegisterStartupScript(base.Page, this.GetType(), Key, keyValue, true);

            if (TotalPageCount <= Convert.ToInt16(CurrentState.Value))
            {
                CurrentState.Value = "0";
                PreviousState.Value = "0";
            }

        }
    }
    public void ReInitializeState()
    {
        CurrentState.Value = "0";
        PreviousState.Value = "0";
    }


    public void FillList()
    {
        if (ControlPath != null)
        {
            lstSlider.ItemTemplate = Template;

            lstSlider.GroupItemCount = PageSize;
            lstSlider.DataSource = DataSource;

            lstSlider.SelectedItemTemplate = SelectedTemplate;
            lstSlider.EmptyDataTemplate = EmptyTemplate;
            lstSlider.SelectedIndex = SelectedIndex;
            if (CssClass != null)
            {
                ScrollingDIV.Attributes["class"] = CssClass;
            }
            lstSlider.DataBind();


            if (IsPaging != false)
            {
                PagerRendering(TotalPageCount);
            }
            if (ProgressBarId != null)
            {
                Panel pnlProgressBarId = lstSlider.NamingContainer.NamingContainer.FindControl(ProgressBarId) as Panel;
                if (pnlProgressBarId != null)
                {
                    ProgressBarId = pnlProgressBarId.ClientID;
                }
            }

            // ReInitializeJavaValue();
        }
    }
    void PagePreRender(object sender, EventArgs e)
    {

    }

    //List View ItemDataBound event expose the itemDatabound event..
    protected void LstSliderItemDataBound(object sender, ListViewItemEventArgs e)
    {

        ItemDatabound(sender, e);

    }

    public IList<ListViewDataItem> Items_
    {
        get
        {
            return lstSlider.Items;
        }
    }


    //Insert Paging in Paging Container depend upon the above property..
    protected void PagerRendering(int PageCount)
    {
        Panel navigation = lstSlider.NamingContainer.NamingContainer.FindControl(PagingContainerId) as Panel;
        if (navigation != null)
        {
            navigation.Controls.Clear();
            for (int i = 1; i < PageCount + 1; i++)
            {
                HyperLink hyp = new HyperLink();
                hyp.NavigateUrl = Request.RawUrl + "#page-" + i; //+ Convert.ToString(i);//"javascript:;";
                hyp.ToolTip = "Page " + Convert.ToString(i);
                hyp.Attributes["af"] = Convert.ToString(i);

                hyp.Attributes.Add("onclick", "RequestToScroll(" + i + ")");

                if (PagingInImages == true)
                {
                    Image img = new Image { AlternateText = "Page " + Convert.ToString(i) };

                    img.ImageUrl = i == 1 ? SelectedImageUrl : UnSelectedImageUrl;
                    hyp.Controls.Add(img);
                }
                else
                {
                    if (i == 1)
                    {
                        hyp.Style["color"] = "#000";
                        hyp.Text = Convert.ToString((i) + @"&nbsp;");
                    }
                    else
                        hyp.Text = Convert.ToString((i) + @"&nbsp;");
                }
                navigation.Controls.Add(hyp);
            }

            PagingContainerId = navigation.ClientID;
        }
    }

    //List View ItemCommand event expose the ItemClick event..
    protected void lstSlider_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        ItemClick(sender, e);
    }
}